package com.weisy.signin.ui.activity;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.squareup.picasso.Picasso;
import com.weisy.signin.R;
import com.weisy.signin.base.BaseAcativity;
import com.weisy.signin.bean.Student;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.utils.ImageUtils;
import com.weisy.signin.widget.CircleTransform;
import com.weisy.signin.widget.LoadingDialog;

import org.litepal.LitePal;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 添加成绩
 * @date 2019/3/26 21:22
 */
@Route(path = Comm.ROUTE_ADD_SCORE)
public class AddScoreActivity extends BaseAcativity {
    @BindView(R.id.img_add_head)
    ImageView imgHead;
    @BindView(R.id.txt_add_id)
    TextView txtId;
    @BindView(R.id.txt_real_name)
    TextView txtName;
    @BindView(R.id.edit_add_score)
    EditText editScore;
    @BindView(R.id.edit_add_midscore)
    EditText editMidScore;
    @BindView(R.id.edit_add_endscore)
    EditText editEndScore;

    @Autowired
    Student student;

    @Override
    public void findView() {
        setContentView(R.layout.activity_add_score);
        ARouter.getInstance().inject(this);
    }

    @Override
    public void initView() {
        if (null != student) {
            txtId.setText(student.getStudentId());
            if (TextUtils.isEmpty(student.getName())) {
                txtName.setText("未设置");
            } else {
                txtName.setText(student.getName());
            }
            if (!TextUtils.isEmpty(student.getImgUrl())) {
                Bitmap bp = ImageUtils.str2Bitmap(student.getImgUrl());
                Picasso.get().load(ImageUtils.bit2Uri(context, bp)).transform(new CircleTransform())
                        .into(imgHead);
            }
            if (0f != student.getScore()) {
                editScore.setText(String.format(Locale.CHINESE, "%.1f", student.getScore()));
            }
            if (0f != student.getMidScore()) {
                editMidScore.setText(String.format(Locale.CHINESE, "%.1f", student.getMidScore()));
            }
            if (0f != student.getEndScore()) {
                editEndScore.setText(String.format(Locale.CHINESE, "%.1f", student.getEndScore()));
            }
        }
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.img_add_close, R.id.btn_add_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_add_close:
                finish();
                break;
            case R.id.btn_add_submit:
                submit();
                break;
        }
    }

    private void submit() {
        if (!TextUtils.isEmpty(editScore.getText().toString().trim())) {
            student.setScore(Float.parseFloat(editScore.getText().toString().trim()));
        }
        if (!TextUtils.isEmpty(editMidScore.getText().toString().trim())) {
            student.setMidScore(Float.parseFloat(editMidScore.getText().toString().trim()));
        }
        if (!TextUtils.isEmpty(editEndScore.getText().toString().trim())) {
            student.setEndScore(Float.parseFloat(editEndScore.getText().toString().trim()));
        }
        final LoadingDialog loadingDialog = new LoadingDialog.Builder(context)
                .setMsg("提交中...")
                .setBgDark(true)
                .create();
        loadingDialog.show();
        new Handler().postDelayed(() -> {
            if (loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
            if (student.updateAll("studentId=?", student.getStudentId())>0) {
                new LoadingDialog.Builder(context)
                        .setMsg("提交成功")
                        .setType(LoadingDialog.TYPE_SUCCESS)
                        .setOnDismissListener((dialog, which) -> {
                            finish();
                        }).create().show();
            } else {
                ToastUtils.show("提交失败");
            }
        }, 1000);
    }
}
